package com.xykj.enterprise.wechat.ext.api.contacts.member;

import com.xykj.enterprise.wechat.bean.ext.contacts.member.CreateUserResp;
import com.xykj.enterprise.wechat.ext.service.contacts.ContactsUserService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Function(description = "创建成员", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "应用场景标识", required = true),

        @Parameter(name = "userid", type = Type.String, description = "账号", required = true),
        @Parameter(name = "name", type = Type.String, description = "姓名", required = true),
        @Parameter(name = "mobile", type = Type.String, description = "手机号", required = true),
        @Parameter(name = "email", type = Type.String, description = "邮箱", required = true),
        @Parameter(name = "department", type = Type.String, description = "部门", required = true),
})
@Component
public class CreateUser implements Action {

    @Autowired
    private ContactsUserService contactsUserService;

    @Override
    public Response execute(Request request) throws Exception {

        String userid = request.getString("userid");
        String name = request.getString("name");
        String mobile = request.getString("mobile");
        String email = request.getString("email");
        String department = request.getString("department");
        CreateUserResp createUserResp = contactsUserService.createUser(
                request.getString("access_token"), userid, name, mobile, email, department
        );
        if (createUserResp.getErrcode() != 0) {
            return Response.fail(createUserResp.getErrmsg());
        }
        return Response.success();
    }
}
